﻿using System.Web;
using eCommerce.core.Helpers;
using eCommerce.db.Cart;

namespace eCommerce.web.Handlers
{
    public class eCommerceHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string command = "";
            if (context.Request.RequestType == "POST")
            {
                if (context.Request["command"] != null)
                {
                    command = context.Request["command"];
                }
                else
                {
                    return;
                }
            }
            else
            {
                if (context.Request.QueryString["command"] != null)
                {
                    command = context.Request.QueryString["command"];
                }
                else
                {
                    return;
                }
            }

            switch (command.ToLower())
            {
                case "additem":
                    AddItem(context);
                    break;
                case "removeitem":
                    RemoveItem(context);
                    break;
            }                
        }

        public void AddItem(HttpContext context)
        {
            if (context.Request[Utils.Id] != null)
            {
                var id = Utils.ToInt32(context.Request[Utils.Id]);
                if (id > 0)
                {
                    try
                    {
                        ShoppingCart.Instance.AddItem(id);
                        context.Response.ContentType = "application/json";
                        context.Response.Write("success");
                    }
                    catch
                    {
                        context.Response.ContentType = "application/json";
                        context.Response.Write("false");
                    }
                }
            }
        }

        public void RemoveItem(HttpContext context)
        {
            if (context.Request[Utils.Id] != null)
            {
                var id = Utils.ToInt32(context.Request[Utils.Id]);
                if (id > 0)
                {
                    try
                    {
                        ShoppingCart.Instance.RemoveItem(id);
                        context.Response.ContentType = "application/json";
                        context.Response.Write("success");
                    }
                    catch
                    {
                        context.Response.ContentType = "application/json";
                        context.Response.Write("false");
                    }
                }
            }    
        }

        public void PostComment(HttpContext context)
        {
            if (context.Request[Utils.Id] != null && context.Request[Utils.TypeId] != null)
            {
                try
                {
                    var id          = Utils.ToInt32(context.Request[Utils.Id]);
                    var typeId      = Utils.ToInt32(context.Request[Utils.TypeId]);
                    var fullName    = context.Request[Utils.Name];
                    var email       = context.Request[Utils.Email];
                    var content     = context.Request[Utils.Content];

                    if (id > 0 && typeId > 0)
                    {
                        //var o = new CommentEntity();
                        //o.Id = id;
                        //o.ParentId = 0;
                        //o.TypeId = typeId;
                        //o.Status = (int)CommentType.IsWaitingForPublish;
                        //o.UserName = fullName;
                        //o.Email = email;
                        //o.Avatar = Constants.Avatar_Default;
                        //o.Body = content;

                        //CommentController.Comment_Create(o);

                        context.Response.ContentType = "application/json";
                        context.Response.Write("success");
                    }
                }
                catch
                {
                    context.Response.ContentType = "application/json";
                    context.Response.Write("false");
                }
            }
        }
    
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}